package com.example.music_project.fragment;

import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.music_project.R;
import com.example.music_project.dao.UserDbHelper;
import com.example.music_project.pages.LoginActivity;

public class ProfileFragment extends Fragment {
    private TextView tvUsername;
    private TextView tvEmail;
    private Button btnLogout;
    private Button btnEditProfile; // 新增：编辑资料按钮
    private UserDbHelper userDbHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);

        userDbHelper = new UserDbHelper(requireContext());
        tvUsername = view.findViewById(R.id.tv_username);
        tvEmail = view.findViewById(R.id.tv_email);
        btnLogout = view.findViewById(R.id.btn_logout);
        btnEditProfile = view.findViewById(R.id.btn_edit_profile); // 确保布局中有此按钮

        // 从 SharedPreferences 加载用户名
        SharedPreferences sharedPreferences = requireActivity().getSharedPreferences("user_data", MODE_PRIVATE);
        String username = sharedPreferences.getString("username", "");
        String email = userDbHelper.getEmailByUsername(username);

        tvUsername.setText("用户名: " + username);
        tvEmail.setText("邮箱: " + email);

        // 退出登录
        btnLogout.setOnClickListener(v -> {
            Intent intent = new Intent(requireActivity(), LoginActivity.class);
            startActivity(intent);
            requireActivity().finish();
        });

        // 跳转到编辑资料页面
        btnEditProfile.setOnClickListener(v -> {


            // 手动切换 Fragment
            Bundle bundle = new Bundle();
            bundle.putString("username", username); // 传递当前用户名
            EditProfileFragment editFragment = new EditProfileFragment();
            editFragment.setArguments(bundle);
            getParentFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, editFragment)
                .addToBackStack(null)
                .commit();
        });

        // 监听来自 EditProfileFragment 的结果
        getParentFragmentManager().setFragmentResultListener(
                "update_profile",
                this,
                (requestKey, bundle) -> {
                    if (requestKey.equals("update_profile")) {
                        String updatedUsername = bundle.getString("username");
                        // 重新加载用户数据
                        loadUserData(updatedUsername);
                    }
                }
        );


        return view;
    }

    /**
     * 加载用户数据并更新 UI
     */
    private void loadUserData(String username) {
        String email = userDbHelper.getEmailByUsername(username);
        tvUsername.setText("用户名: " + username);
        tvEmail.setText("邮箱: " + email);

        // 更新 SharedPreferences 中的用户名
        SharedPreferences.Editor editor = requireActivity().getSharedPreferences("user_data", MODE_PRIVATE).edit();
        editor.putString("username", username);
        editor.apply();
    }
}